package com.lagou.edu.springbootrabbit.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    /**
     * 设置普通队列
     *
     * @return
     */
    @Bean
    public Queue myQueue() {
        Map<String, Object> props = new HashMap<>();
        props.put("x-message-ttl", 30000); //消息生存时间30s
        //设置该队列关联的死信队列交换器
        props.put("x-dead-letter-exchange", "ex.deadpay.dlx");
        //该队列往私信队列发送时候的routingKey,如果没指定，使用原队列的routingKey
        props.put("x-dead-letter-routing-key", "go.deadpay.dlx");

        //参数： 队列名，是否持久化，是否排他，是否自动删除，属性
        return new Queue("myqueue", true, false, false, props);
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue dlxQueue() {
        return new Queue("dlxqueue", true, false, false,null);
    }

    @Bean
    public Exchange myExchange() {
        return new TopicExchange("ex.orderpay.dlx", true, false,null);
    }

    /**
     * 死信交换器
     * @return
     */
    @Bean
    public Exchange dlxExchange() {
        return new TopicExchange("ex.deadpay.dlx", true, false, null);
    }

    /**
     * 正常队列和交换机绑定
     * @return
     */
    @Bean
    public Binding binding(){
        return BindingBuilder.bind(myQueue()).to(myExchange()).with("go.orderpay.dlx").noargs();
    }

    /**
     * 死信队列和死信交换器绑定
     */
    @Bean
    public Binding dlxBinding(){
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with("go.deadpay.dlx").noargs();
    }
}